#include <gtk/gtk.h>

GtkWidget  *window;
void showDialog(GtkWidget *widget,  gpointer *data)
{


    GtkBuilder *builder = NULL;
    GtkWidget  *dialog;
    gint res;
    builder = gtk_builder_new ();
    gtk_builder_add_from_file (builder, "glade.glade", NULL);
    dialog = GTK_WIDGET (gtk_builder_get_object (builder, "filechooserdialog"));

    res = gtk_dialog_run (GTK_DIALOG (dialog));
    if (res == GTK_RESPONSE_OK)
    {
        printf("点击了确定按钮\n");
        char *filename;
        GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
        filename = gtk_file_chooser_get_filename (chooser);
        printf ("%s\n",filename);
        g_free (filename);
    }
    else if( res == GTK_RESPONSE_CANCEL )
    {
        printf("点击了取消按钮\n");
    }

    gtk_widget_destroy( GTK_WIDGET (dialog));
    g_object_unref (G_OBJECT (builder));

}


void showDialogTest(GtkWidget *widget,  gpointer *data)
{
    gint res;

    res = gtk_dialog_run (GTK_DIALOG (data));
    if (res == GTK_RESPONSE_OK)
    {
        printf("点击了确定按钮\n");
        char *filename;
        GtkFileChooser *chooser = GTK_FILE_CHOOSER (data);
        filename = gtk_file_chooser_get_filename (chooser);
        printf ("%s\n",filename);
        g_free (filename);
    }
    else if( res == GTK_RESPONSE_CANCEL )
    {
        printf("点击了取消按钮\n");
    }

    gtk_widget_hide(GTK_WIDGET (data));
}

void select_changed(GtkWidget *widget,  gpointer *data)
{
    char *filename;
    GtkFileChooser *chooser = GTK_FILE_CHOOSER (widget);
    filename = gtk_file_chooser_get_filename (chooser);
    gtk_label_set_text(GTK_LABEL(data),filename);
}


int main (int argc, char *argv[])
{
    GtkBuilder *builder = NULL;
    GtkWidget  *dialog;
    gtk_init (&argc, &argv);
 
    builder = gtk_builder_new ();
    gtk_builder_add_from_file (builder, "glade.glade", NULL);

    gtk_builder_connect_signals (builder, NULL);
    window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
    dialog = GTK_WIDGET (gtk_builder_get_object (builder, "filechooserdialogTest"));
    
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    

    g_object_unref (G_OBJECT (builder));
    gtk_widget_show_all(window);                
    gtk_main ();
 
    return 0;
}